package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dao.DishDto;
import com.itheima.reggie.dao.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name){
        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        //创建lambda表达式
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        // 添加排序条件
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行查询
        setmealService.page(pageInfo,wrapper);

        List<Setmeal> records = pageInfo.getRecords();

        //new一List集合
        List<DishDto> dishDtos = new ArrayList<>();
        records.forEach(setmeal -> {
            //
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(setmeal,dishDto);
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Category::getId,setmeal.getCategoryId());
            System.out.println("----------========================");
            System.out.println(setmeal.getCategoryId());
            Category one = categoryService.getOne(queryWrapper);
            //if(one!=null)
                dishDto.setCategoryName(one.getName());
           // dishDto.setCategoryName("1");
            dishDtos.add(dishDto);
        });
        Page<DishDto> dishDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        dishDtoPage.setRecords(dishDtos);
        return R.success(dishDtoPage);
    }

    /**
     * 套餐添加
     * @param setmealDto
     * @return
     */
   @PostMapping
   @CacheEvict(value = "Setmeal", allEntries = true)
   public R<String> save(@RequestBody SetmealDto setmealDto){

       setmealService.saveWithDish(setmealDto);

       return R.success("新增套餐成功");
   }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 批量起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids){
        // 通过id查询数据库。修改id为ids数组中的数据的菜品状态status为前端页面提交的status。
//                for (Long id : ids) {
//                        //根据id得到每个dish菜品。
//                        Dish dish = dishService.getById(id);
//                        dish.setStatus(status);
//                        dishService.updateById(dish);
//                }
//

        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(Setmeal::getId,ids).set(Setmeal::getStatus,status);
        setmealService.update(wrapper);
        return R.success("修改成功");

    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto setmealDish = setmealDishService.getByIdWithFlavor(id);
        return R.success(setmealDish);
    }


    /**
     * 修改菜品
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){

        setmealDishService.updateWithFlavor(setmealDto);
        return R.success("修改菜品成功");
    }

    @GetMapping("/list")
    @Cacheable(value = "Setmeal" ,key = "'Setmeal_'+ #setmeal.getCategoryId()",unless = "#result == null ")
    public R<List<Setmeal>>  list(Setmeal setmeal){

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }

}
